<?php
class m_rekap_keuangan_bulanan extends Model{
    var $table = "rekap_keuangan_bulanan";
    
    function getKeuanganDonaturTunggal($bulan,$tahun){
        $tanggal = betweenDate($bulan, $tahun);
        $idDonaturTunggal = $this->db->query("SELECT kdt.id,kdt.id_donatur_tunggal,m.nama FROM `keuangan_donatur_tunggal` kdt 
                                            join member m on kdt.id_donatur_tunggal = m.id
                                            where kdt.tanggal between '$tanggal[awal]' and '$tanggal[akhir]' group by kdt.id_donatur_tunggal");
        $result = array();
        if($idDonaturTunggal->num_rows() > 0){
            $i = 0;
            foreach ($idDonaturTunggal->result_array() as $donatur){
                $result[$i]['donaturTunggal'] = $donatur;
                $detail = $this->db->query("select kdt.*,m.nama as petugas from keuangan_donatur_tunggal kdt
                                            join member m on kdt.id_petugas = m.id
                                            where kdt.tanggal between '$tanggal[awal]' and '$tanggal[akhir]' and kdt.id_donatur_tunggal = $donatur[id_donatur_tunggal]");
                if($detail->num_rows() > 0){
                    foreach ($detail->result_array() as $det){
                        $result[$i]['detail'][] = $det;
                    }
                }else{
                    $result[$i]['detail'][] = array();
                }
                $i++;
            }
        }
        return $result;
    }
    function cekRekapKeuanganBulanan($id){
       $query = $this->db->query("select * from rekap_keuangan_bulanan where id = '$id'");
       return $query->num_rows();
    }
    function addRekapKeuanganBulanan($data){
          foreach ($data['id_keuangan_donatur_tunggal'] as $kdt){
              $this->db->query("update keuangan_donatur_tunggal set status = '1' where id = '$kdt'");
          }
          foreach ($data['id_collecting_day'] as $icd){
              $this->db->query("update collecting_day set status = '1' where id = '$icd'");
          }
          foreach ($data['id_pemberian_beasiswa'] as $ipb){
              $this->db->query("update pemberian_beasiswa set status = '1' where id = '$ipb'");
          }
          foreach ($data['id_pemasukan'] as $pms){
              $this->db->query("update pemasukan set status = '1' where id = '$pms'");
          }
          foreach ($data['id_pengeluaran'] as $png){
              $this->db->query("update pengeluaran set status = '1' where id = '$png'");
          }
        $selisih = $data['pemasukan']-$data['pengeluaran']; 
        $cek = $this->cekRekapKeuanganBulanan($data['id']);
        if($cek == 0){
            $isi = array('id' => $data['id'],'jumlah' => $selisih);
            $exe = $this->db->insert($this->table,$isi);
        }else{
            $exe = $this->db->query("update ".$this->table." set jumlah = (jumlah+$selisih) where id = '$data[id]'");
        }
        
        if($exe){
            return TRUE;
        }else return FALSE;
    }
    function getListRekapKeuanganBulanan($params){
        $bln1 = $params['bln1'];
        $bln2 = $params['bln2'];
        $thawal = $params['thawal'];
        $thakhir = $params['thakhir'];
        
        $selisih = $thakhir - $thawal;
        for ($i = blnAngka($bln1); $i <= blnAngka($bln2) + ($selisih * 12); $i++) {
             if(count($i) == 1) $bulan = "0".$i;
             else $bulan = $i;
                $num = 0;
                    if ($i > (12 * $num)) {
                        $tahun = $thawal + $num;
                    } else {
                        $tahun = $thawal;
                    }
                    $num += 1;
                    $query = $this->db->query("SELECT * FROM rekap_keuangan_bulanan where id = '".$bulan.$tahun."'");
                    $data[] = $query->result_array();
         }
         $jumlah = array();
         foreach ($data as $key => $row){
             if($row[0]['jumlah'] == ""){
                 $jumlah[] = 0;
             }else $jumlah[] = $row[0]['jumlah'];
         }
         $return['max'] = max($jumlah);
         $return['master'] = $data;
         return $return;
    }
    function getListRekapKeuanganTahunan($params){
        $selisih = ($params['thakhir'] - $params['thawal']);
        
        $thn = $params['thawal'];
        for($i = 0; $i <= $selisih; $i++){
            $jumlah = 0;
            for($j = 1;$j <= 12; $j++){
                if(strlen($j) == 1){$k = '0'.$j;}
                else $k = $j;
                
                $query = $this->db->query("SELECT * FROM rekap_keuangan_bulanan where id = '".$k.$thn."'");
                $data = $query->result_array();
                foreach ($data as $row){
                    $jumlah = $jumlah + $row['jumlah'];
                }
            }
            $result['jumlah'][] = $jumlah;
            $return['master'][] = array('tahun' => $thn,'jumlah' => $jumlah);
            $thn++;
        }
        $return['max'] = max($result['jumlah']);
        return $return;
    }
    function getAllSaldo(){
        $query = $this->db->query("select sum(jumlah) as saldo from rekap_keuangan_bulanan");
        $saldo = $query->result_array();
        
        return $saldo;
    }
}
?>
